/*
 * @(#)NewsServiceImpl.java 2011-11-08
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.domain.CompanyInfo;
import cn.shangjia.common.domain.Shop;
import cn.shangjia.common.domain.User;
import cn.shangjia.common.domain.UserContactOnline;
import cn.shangjia.common.domain.UserInfo;
import cn.shangjia.common.domain.support.State;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.dao.UserContactOnlineDao;
import cn.shangjia.shop.dao.UserDao;
import cn.shangjia.shop.exception.NotFoundException;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.CompanyInfoService;
import cn.shangjia.shop.service.ShopInfoService;
import cn.shangjia.shop.service.UserInfoService;

/**
 * 服务实现.
 *
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-08 18:13:36
 * @since 1.0
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	private static final Logger logger = ShopLoggerFactory.getLogger(UserInfoService.class);
	
	@Resource
	private UserDao userDao;
	
	@Resource
	private UserContactOnlineDao userContactOnlineDao;
	
	@Resource
	private CompanyInfoService companyInfoService;
	
	@Resource
	private ShopInfoService shopInfoService;
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.UserInfoService#getUser(java.lang.String)
	 */
	@Override
	public User getUser(String username) {
		if (StringUtils.isEmpty(username))
			throw new NotFoundException();
		return userDao.read(username);
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.UserInfoService#getUserContactOnline(java.lang.String)
	 */
	@Override
	public UserContactOnline getUserContactOnline(String userId) {
		return userContactOnlineDao.read(userId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.UserInfoService#getUserInfo(java.lang.String)
	 */
	@Override
	public UserInfo getUserInfo(String username) {
		
		UserInfo userInfo = new UserInfo();
		
		User user = getUser(username);
		
		if (user == null) {
			logger.warn("用户名在商家网内数据不存在. 请核对数据! username = ", username);
			return null;
		}
		
		if (State.UNACTIVATED.equals(user.getState())) {
			return null;
		}
		
		//读取用户联系信息
		UserContactOnline userContact = getUserContactOnline(user.getId());
		
		//读取用户企业信息
		CompanyInfo companyInfo = companyInfoService.getCompanyInfoByUserId(user.getId());
		//读取用户商铺信息
		Shop shop = shopInfoService.getShopByUserId(user.getId());
		
		userInfo.setUser(user);
		userInfo.setUserContact(userContact);
		userInfo.setShop(shop);
		userInfo.setCompanyInfo(companyInfo);
		
		return userInfo;
	}
}
